package de.baumann.browser.activitys.oc

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import de.baumann.browser.R
import de.baumann.browser.adapter.OCBAdapter
import de.baumann.browser.api.net.vo.Coin
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.iview.IOCBView
import de.baumann.browser.present.OCBPresenter
import de.baumann.browser.utils.*
import de.baumann.browser.utils.web3j.WalletDaoUtils
import kotlinx.android.synthetic.main.activity_o_c_b.*

class OCBActivity : BaseOdinActivity<IOCBView, OCBPresenter>(), IOCBView {

    companion object {
        fun startOCBActivity(context: Context, wallet: ETHWallet?) {
            var ethWallet: ETHWallet? = null
            if (wallet == null) {
                val user = getUser()
                if (user != null) {
                    var address = user.eth_address
                    if (address != null) {
                        if (address.startsWith("odx")) {
                            address = "0${address.substring(2)}"
                        }
//                        ethWallet = WalletDaoUtils.queryWalletByAddress(address)
                    }
                }
                if (getMoneyAddress() != null) {
                    ethWallet = WalletDaoUtils.queryWalletByAddress(getMoneyAddress())
                }
                if (ethWallet == null) {
                    ethWallet = WalletDaoUtils.loadAll()[0]
                }
            } else {
                ethWallet = wallet
            }
            context.startActivity(Intent(context, OCBActivity::class.java).apply {
                val bundel = Bundle().apply {
                    putSerializable("odinWallet", ethWallet)
//                    saveETHWallet(ethWallet!!)
                }
                putExtras(bundel)
            })
        }
    }

    private lateinit var oCBAdapter: OCBAdapter

    override fun getLayout(): Int {
        return R.layout.activity_o_c_b
    }

    override fun initView() {
        commTool.setNavigationIcon(R.drawable.icon_home_back)
        commTitles.text = "ODIN钱包"
        setSupportActionBar(commTool)
        commTool.setNavigationOnClickListener {
            finish()
        }
        qblb.setOnClickListener {
//            OcWalletListActivity.startWalletListActivity(mContext)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_wallet_setting, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
//        OcWalletSettingActivity.startOcWalletSettingActivityForResult(mContext, getWallet())
        return super.onOptionsItemSelected(item)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (WalletDaoUtils.loadAll().size > 0) {
                saveMoneyAddress(WalletDaoUtils.loadAll()[0].address)
                saveWalletName(WalletDaoUtils.loadAll()[0].address)
                presenter.login()
                finish()
            }
            WalletDaoUtils.loadAll()[0].address
            finish()
        }
    }

    override fun initData() {
//        val waller = getWallet()
//        val a = waller.address
//        qbmc.text = WalletDaoUtils.getWalletName(getMoneyAddress())
//        qbdz.text = StringUtil.StrReplace(getMoneyAddress())
//        oCBAdapter = OCBAdapter(a)
//        ocbList.adapter = oCBAdapter
//        oCBAdapter.setOnItemClickListener { _, _, position ->
//            val coin = oCBAdapter.getItem(position)
//            if (coin!!.name.toUpperCase() == "ODIN") {
//                OcWalletDetailActivity.startOcWalletDetailActivity(mContext, getWallet())
//            } else {
//                OCBDetailActivity.startOCBDetailActivity(mContext, getWallet().name, getWallet().address, coin.name, coin.node, coin.contract)
//            }
//        }
//        ocbdc.setOnClickListener {
//            if (a.startsWith("0x")) {
//                StringUtil.copy(mContext, "odx${a.substring(2)}")
//            } else {
//                StringUtil.copy(mContext, a)
//            }
//            showShortToast("已复制到剪切板")
//        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        initData()
        presenter?.getList()
    }

    override fun onResume() {
        super.onResume()
        val waller = getMoneyAddress()
        val a = getMoneyAddress()
        qbmc.text = WalletDaoUtils.getWalletName(waller)
        qbdz.text = StringUtil.StrReplace(waller)
        oCBAdapter = OCBAdapter(waller!!)
        ocbList.adapter = oCBAdapter
        oCBAdapter.setOnItemClickListener { _, _, position ->
            val coin = oCBAdapter.getItem(position)
            if (coin!!.name.toUpperCase() == "ODIN") {
                OcWalletDetailActivity.startOcWalletDetailActivity(mContext, getWallet())
            } else {
                OCBDetailActivity.startOCBDetailActivity(mContext, getWallet().name, getWallet().address, coin.name, coin.node, coin.contract)

            }
        }
        ocbdc.setOnClickListener {
            if (a!!.startsWith("0x")) {
                StringUtil.copy(mContext, "odx${a!!.substring(2)}")
            } else {
                StringUtil.copy(mContext, a)
            }
            showShortToast("已复制到剪切板")
        }
        presenter?.getList()
    }

    private fun getWallet(): ETHWallet {
//        return intent.getSerializableExtra("odinWallet") as ETHWallet
        return getETHWallet()!!
    }

    override fun createPresenter(): OCBPresenter {
        return OCBPresenter()
    }

    override fun createView(): IOCBView {
        return this
    }

    override fun setCoinList(list: List<Coin>) {
        oCBAdapter.setNewData(list)
    }
}